<template>
	<view class="wrap">
		<view class="form">
			<view class="form-item">
				<view class="form-item-lable">手机号</view>
				<view class="input">
					<view class="phone">{{ userinfo.phone || phone }}</view>
					<view class=""></view>
				</view>
			</view>

			<view class="form-item">
				<view class="form-item-lable">验证码</view>
				<view class="input">
					<input type="text" v-model="form.code" placeholder="请输入验证码" placeholder-class="placeholder" />
					<view class="link" v-if="showCode" @click="getPhoneCode">{{ tips }}</view>
				<view v-else class="text">
					{{setp}}s后重发
				</view>
				</view>
			</view>

			<view class="form-item">
				<view class="form-item-lable">新密码</view>
				<view class="input">
					<input v-if="eye" v-model="form.newPassword" :type="'text'" placeholder="请输入密码,至少6位" placeholder-class="placeholder" />
						<input v-else v-model="form.newPassword" :type=" 'password'" placeholder="请输入密码,至少6位" placeholder-class="placeholder" />
					<image @click.stop="eye = !eye" :src="eye ? '/static/images/eye-open.png' : '/static/images/eye-close.png'" mode=""></image>
				</view>
			</view>

			<view class="form-item">
				<view class="form-item-lable">再次输入</view>
				<view class="input">
					<input v-if="eyeagn" v-model="form.newPassword1" :type=" 'text' " placeholder="请再次输入密码" placeholder-class="placeholder" />
					<input v-if="!eyeagn" v-model="form.newPassword1" :type=" 'password' " placeholder="请再次输入密码" placeholder-class="placeholder" />
					<image @click.stop="eyeagn = !eyeagn" :src="eyeagn ? '/static/images/eye-open.png' : '/static/images/eye-close.png'" mode=""></image>
				</view>
			</view>
		</view>

		<view :class="disabled ? 'btn disabled' : 'btn'" @click="submit">提交</view>

		<view class="tips">
			<view>未收到验证码？</view>
			<view class="">1.通讯延迟，请耐心等待一下；</view>
			<view>2.短信通道屏蔽了您的手机号码，请在获取验证码倒计时结束后重新获取，我们将其切换通道再次发送。如果多次发送均未收到，请联系客服0760-88833388</view>
		</view>
	</view>
</template>

<script>
import { mapState, mapActions } from 'vuex';
import { getPhoneCode, reSetPassword } from '@/apis/login.js';
import md5 from 'js-md5';
export default {
	data() {
		return {
			eye: false,
			eyeagn: false,
			form: {
				phone: '',
				code: '',
				captchaId: '',
				newPassword: '',
				newPassword1: ''
			},
			setp: 60,
			showCode: true,
			tips: '获取验证码',
			timeDown: null,
			phone:''
		};
	},
	computed: {
		...mapState('user', ['userinfo']),
		disabled() {
			if (!this.form.code ) {
				return 1;
			}

			if (!this.form.newPassword || !this.form.newPassword1) {
				return 2;
			}

			if (this.form.newPassword != this.form.newPassword1) {
				return 3;
			}
			
			if (this.form.newPassword.length < 6 || this.form.newPassword.length > 8) {
				return 4;
			}

			return false;
		}
	},
	onLoad(os) {
		this.phone = os.phone || '';
	},
	methods: {
		// 提交
		submit() {
			if (this.disabled) {
				uni.showToast({
					title:['请输入验证码','请输入密码与确认密码','新密码与确认密码不相同','密码需为6-8位'][this.disabled - 1],
					icon:'none'
				
				});
				return;
			}
			this.form.phone = this.userinfo.phone || this.phone;
			let form = JSON.parse(JSON.stringify(this.form));
			form.newPassword = md5(form.newPassword);
			form.newPassword1 = md5(form.newPassword1);
			console.log(form);
			// return 
			uni.showLoading({
				title:'提交中',
				mask:true
			});
			reSetPassword(form).then(res=>{
				uni.hideLoading();
				uni.showToast({
					title:'修改成功',
					mask:true
				});
				setTimeout(()=>{
					uni.navigateBack({
						
					});
				},700);
			});
		},
		// 设置倒计时
		setTimeDown() {
			if (this.timeDown) {
				clearInterval(this.timeDown);
			}
			this.setp = 60;
			this.showCode = false;
			let _this = this;
			this.timeDown = setInterval(function() {
				if (_this.setp <= 1) {
					_this.tips = '再次获取';
					_this.showCode = true;
				} else {
					_this.setp--;
				}
			}, 1000);
		},
		// 获取验证码
		getPhoneCode() {
			uni.showLoading({
				title:'发送中',
				mask:true
				
			});
			getPhoneCode(this.userinfo.phone || this.phone).then(res => {
				this.form.captchaId = res.data.id;
				this.setTimeDown();
				uni.hideLoading();
			});
		}
	}
};
</script>

<style lang="scss">
page {
	background: #f0f0f0;
}
.placeholder {
	color: #ccc;
	font-weight: normal;
}
.form {
	width: 100%;
	background-color: #fff;
	color: #333;
	&-item {
		width: calc(100% - 72rpx);
		padding: 36rpx 0;
		margin: 0 auto;
		border-bottom: 2rpx solid #eeeeee;
		&-lable {
			font-size: 26rpx;
			text-align: left;
			color: #999999;
			margin-bottom: 5rpx;
		}
		.input {
			display: flex;
			justify-content: space-between;
			align-items: center;

			input {
				width: 70%;
				font-size: 32rpx;
				font-weight: bold;
			}
			image {
				width: 35rpx;
				height: 35rpx;
			}
			.phone {
				font-size: 32rpx;
				font-weight: bold;
			}
			.text{
				font-size: 28rpx;
				color: #999;
			}
			.link {
				color: #0091ff;
				font-size: 28rpx;
			}
		}
	}
}

.btn {
	width: 670rpx;
	height: 84rpx;
	opacity: 1;
	background: #0091ff;
	border-radius: 8rpx;
	text-align: center;
	font-size: 28rpx;
	color: #fff;
	line-height: 84rpx;
	margin: 30rpx auto;
	transition: 0.4s all;
}
.disabled {
	background: #e1e1e1;
	color: #ccc;
}

.tips {
	position: absolute;
	bottom: 80rpx;
	left: 40rpx;
	width: 670rpx;
	opacity: 1;
	font-size: 24rpx;
	font-family: PingFangSC, PingFangSC-Regular;
	font-weight: 400;
	text-align: left;
	color: #999999;
	line-height: 34rpx;
	letter-spacing: 0.6rpx;
	view:first-child {
		font-size: 28rpx;
		margin-bottom: 5rpx;
	}
}
</style>
